﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Drawing2D;
using System;
using System.Threading;

namespace MissileCommand
{
    class MissileShots
    {
        private Graphics g;
        private PointF startPoint;
        private PointF endPoint;
        private Boolean active;
        private List<PointF> PointList = new List<PointF>();
        private int pointIndex;
        private Thread boomThread;
        ExplosionBoom boom;



        public MissileShots(int xS, int yS, int xE, int yE, Graphics g)
        {
            startPoint = new Point(xS, yS);
            endPoint = new Point(xE, yE);
            this.g = g;
            this.g.SmoothingMode = SmoothingMode.AntiAlias;
            this.active = true;
            CalcPoints();
            boom = new ExplosionBoom(g, endPoint);
            boomThread = new Thread(new ThreadStart(boom.drawBoom));             

        }

        public void CalcPoints()
        {
            // Get Points From Line(s)
            float curDist = 0;
            float distance = 0;

            PointF ptA = startPoint;
            PointF ptB = endPoint;
            float deltaX = ptB.X - ptA.X;
            float deltaY = ptB.Y - ptA.Y;
            curDist = 0;
            distance = (float)Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
            while (curDist < distance)
            {
                curDist+=6; //adjust speed of line
                float offsetX = (float)((double)curDist / (double)distance * (double)deltaX);
                float offsetY = (float)((double)curDist / (double)distance * (double)deltaY);
                PointList.Add(new PointF(ptA.X + offsetX, ptA.Y + offsetY  ));
            }
            PointList.Add(new PointF(endPoint.X, endPoint.Y));
          }


        public void Draw()
        {
            if (pointIndex < PointList.Count  & this.active)
            {
                for (int i = 0; i < pointIndex; i++)
                {
                    g.DrawLine(Pens.AliceBlue, PointList[i].X, PointList[i].Y, PointList[i + 1].X, PointList[i + 1].Y);
                }
                pointIndex++;
            }
            else if (this.active)// draw circle/boom
            {
                this.active = false;
                //boomThread.Start();
                boom.drawBoom();
                
            }


        }
        /*
        private void drawBoom()
        {
            lock (this)
            {
                SolidBrush mybrush = new SolidBrush(Color.Blue);
                    g.FillEllipse(mybrush, endPoint.X - 20, endPoint.Y - 20, 35, 35);
                    mybrush.Color = Color.Red;
                    g.FillEllipse(mybrush, endPoint.X - 20, endPoint.Y - 20, 35, 35);
                    mybrush.Color = Color.Orange;
                    g.FillEllipse(mybrush, endPoint.X - 20, endPoint.Y - 20, 35, 35);
                    mybrush.Color = Color.Yellow;
                    g.FillEllipse(mybrush, endPoint.X - 20, endPoint.Y - 20, 35, 35);
                    boomThread.Suspend();
            }
        }*/



    }
}
